﻿#include "combobox.h"
#include <QPaintEvent> // 确保 QPaintEvent 类型完整

// ItemDelegate 实现
ItemDelegate::ItemDelegate(QObject *parent)
    : QStyledItemDelegate(parent)
{
}

void ItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    painter->save();
    painter->setRenderHint(QPainter::Antialiasing);

    // 绘制背景
    if (option.state & QStyle::State_Selected) {
        painter->fillRect(option.rect, QColor(0, 120, 212)); // Fluent UI 选中颜色
    } else if (option.state & QStyle::State_MouseOver) {
        painter->fillRect(option.rect, QColor(243, 242, 241)); // Fluent UI 悬停颜色
    } else {
        painter->fillRect(option.rect, Qt::white); // 默认背景
    }

    // 绘制图标和文本
    QRect iconRect = option.rect.adjusted(5, 5, -option.rect.width() + 30, -5);
    QIcon icon = qvariant_cast<QIcon>(index.data(Qt::DecorationRole));
    if (!icon.isNull()) {
        icon.paint(painter, iconRect, Qt::AlignLeft | Qt::AlignVCenter);
    }

    QRect textRect = option.rect.adjusted(30, 0, -5, 0);
    QString text = index.data(Qt::DisplayRole).toString();
    painter->setPen(Qt::black);
    painter->drawText(textRect, Qt::AlignLeft | Qt::AlignVCenter, text);

    painter->restore();
}

QSize ItemDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    Q_UNUSED(option);
    Q_UNUSED(index);
    return QSize(100, 30); // 设置每一项的大小
}

// ComboBox 实现
ComboBox::ComboBox(QWidget *parent)
    : QComboBox(parent), m_backgroundColor(Qt::white), m_borderRadius(5), m_borderColor(Qt::transparent)
{
    setItemDelegate(new ItemDelegate(this)); // 设置自定义的 ItemDelegate
}

void ComboBox::setBackgroundColor(const QColor &color)
{
    m_backgroundColor = color;
    update();
}

void ComboBox::setBorderRadius(int radius)
{
    m_borderRadius = radius;
    update();
}

void ComboBox::setBorderColor(const QColor &color)
{
    m_borderColor = color;
    update();
}

void ComboBox::setIcon(const QIcon &icon)
{
    m_icon = icon;
    update();
}

void ComboBox::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event);

    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);

    // 绘制背景
    painter.setPen(m_borderColor);
    painter.setBrush(m_backgroundColor);
    painter.drawRoundedRect(rect(), m_borderRadius, m_borderRadius);

    // 绘制文本和图标
    QRect textRect = rect().adjusted(5, 0, -20, 0);
    if (!m_icon.isNull()) {
        QRect iconRect = rect().adjusted(5, 0, -rect().width() + 30, 0);
        m_icon.paint(&painter, iconRect, Qt::AlignLeft | Qt::AlignVCenter);
        textRect.adjust(30, 0, 0, 0);
    }
    painter.setPen(Qt::black);
    painter.drawText(textRect, Qt::AlignLeft | Qt::AlignVCenter, currentText());

    // 绘制下拉箭头
    QStyleOptionComboBox opt;
    initStyleOption(&opt);
    opt.rect = rect().adjusted(rect().width() - 20, 0, -5, 0);
    style()->drawPrimitive(QStyle::PE_IndicatorArrowDown, &opt, &painter, this);
}

void ComboBox::showPopup()
{
    QComboBox::showPopup();

    // 获取下拉列表的 QListView
    QListView *listView = qobject_cast<QListView *>(view());
    if (listView) {
        // 设置无边框
        listView->setFrameShape(QFrame::NoFrame);

        // 禁用系统默认的绘制
        listView->setAttribute(Qt::WA_OpaquePaintEvent, false);
        listView->setAttribute(Qt::WA_NoSystemBackground, true);
        listView->viewport()->setAttribute(Qt::WA_OpaquePaintEvent, false);
        listView->viewport()->setAttribute(Qt::WA_NoSystemBackground, true);

        // 设置圆角
        listView->viewport()->setProperty("borderRadius", m_borderRadius);

        // 重写绘制事件
        listView->viewport()->installEventFilter(this);
    }
}

bool ComboBox::eventFilter(QObject *obj, QEvent *event)
{
    // 检查是否是 QListView 的 viewport
    QListView *listView = qobject_cast<QListView *>(view());
    if (listView && obj == listView->viewport()) {
        if (event->type() == QEvent::Paint) {
            QPainter painter(listView->viewport());
            painter.setRenderHint(QPainter::Antialiasing);

            // 绘制背景
            painter.setPen(Qt::NoPen);
            painter.setBrush(Qt::white);
            painter.drawRoundedRect(listView->viewport()->rect(), m_borderRadius, m_borderRadius);

            // 绘制下拉列表项
            QStyleOptionViewItem option;
            option.initFrom(listView);
            for (int i = 0; i < listView->model()->rowCount(); ++i) {
                QModelIndex index = listView->model()->index(i, 0);
                QRect itemRect = listView->visualRect(index);

                // 绘制背景
                if (listView->selectionModel()->isSelected(index)) {
                    painter.fillRect(itemRect, QColor(0, 120, 212)); // 选中状态
                } else if (itemRect.contains(listView->viewport()->mapFromGlobal(QCursor::pos()))) {
                    painter.fillRect(itemRect, QColor(243, 242, 241)); // 悬停状态
                } else {
                    painter.fillRect(itemRect, Qt::white); // 默认状态
                }

                // 绘制图标和文本
                QIcon icon = qvariant_cast<QIcon>(index.data(Qt::DecorationRole));
                if (!icon.isNull()) {
                    QRect iconRect = itemRect.adjusted(5, 5, -itemRect.width() + 30, -5);
                    icon.paint(&painter, iconRect, Qt::AlignLeft | Qt::AlignVCenter);
                }

                QRect textRect = itemRect.adjusted(30, 0, -5, 0);
                QString text = index.data(Qt::DisplayRole).toString();
                painter.setPen(Qt::black);
                painter.drawText(textRect, Qt::AlignLeft | Qt::AlignVCenter, text);
            }

            return true; // 事件已处理
        }
    }
    return QComboBox::eventFilter(obj, event);
}
